package com.jxdinfo.hussar.example.mes.qc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jxdinfo.hussar.example.common.annotation.Log;
import com.jxdinfo.hussar.example.common.constant.UserConstants;
import com.jxdinfo.hussar.example.common.core.controller.BaseController;
import com.jxdinfo.hussar.example.common.core.domain.AjaxResult;
import com.jxdinfo.hussar.example.common.core.page.TableDataInfo;
import com.jxdinfo.hussar.example.common.enums.BusinessType;
import com.jxdinfo.hussar.example.common.utils.poi.ExcelUtil;

import com.jxdinfo.hussar.example.mes.pro.domain.ProFeedback;
import com.jxdinfo.hussar.example.mes.pro.domain.ProProductRecpt;
import com.jxdinfo.hussar.example.mes.pro.domain.ProTask;
import com.jxdinfo.hussar.example.mes.pro.service.IProFeedbackService;
import com.jxdinfo.hussar.example.mes.pro.service.IProProductRecptService;
import com.jxdinfo.hussar.example.mes.pro.service.IProTaskService;
import com.jxdinfo.hussar.example.mes.qc.domain.QcResultRecord;
import com.jxdinfo.hussar.example.mes.qc.domain.QcResultRecordItem;
import com.jxdinfo.hussar.example.mes.qc.service.IQcResultRecordItemService;
import com.jxdinfo.hussar.example.mes.qc.service.IQcResultRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jxdinfo.hussar.example.mes.qc.domain.QcResult;
import com.jxdinfo.hussar.example.mes.qc.service.IQcResultService;


/**
 * 检测结果记录Controller
 *
 * @author 常宝坤
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/qc/qcresult")
public class QcResultController extends BaseController
{
    @Autowired
    private IQcResultService qcResultService;

    @Autowired
    private IProFeedbackService proFeedbackService;

    @Autowired
    private IProProductRecptService proProductRecptService;

    @Autowired
    private IProTaskService proTaskService;

    @Autowired
    private IQcResultRecordService qcResultRecordService;

    /**
     * 查询检测结果记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(QcResult qcResult)
    {
        startPage();
        List<QcResult> list = qcResultService.selectQcResultList(qcResult);
        return getDataTable(list);
    }

    /**
     * 导出检测结果记录列表
     */
    @Log(title = "检测结果记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QcResult qcResult)
    {
        List<QcResult> list = qcResultService.selectQcResultList(qcResult);
        ExcelUtil<QcResult> util = new ExcelUtil<QcResult>(QcResult.class);
        util.exportExcel(response, list, "检测结果记录数据");
    }

    /**
     * 获取检测结果记录详细信息
     */
    @GetMapping(value = "/get/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return AjaxResult.success(qcResultService.selectQcResultByRecordId(recordId));
    }

    /**
     * 新增检测结果记录
     */
    @Log(title = "检测结果记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody QcResult qcResult)
    {
        return toAjax(qcResultService.insertQcResult(qcResult));
    }

    /**
     * 修改检测结果记录
     */
    @Log(title = "检测结果记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody QcResult qcResult)
    {
        return toAjax(qcResultService.updateQcResult(qcResult));
    }

    /**
     * 删除检测结果记录
     */
    @Log(title = "检测结果记录", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(qcResultService.deleteQcResultByRecordIds(recordIds));
    }

    @Log(title = "检验完成执行", businessType = BusinessType.UPDATE)
    @Transactional
    @GetMapping("/{recordId}")
    public AjaxResult execute(@PathVariable("recordId") Long recordId)
    {
        //获取到检验完成的数据行
        QcResult qcResult = qcResultService.selectQcResultByRecordId(recordId);
        //检验完成
        qcResult.setIsComplete("1");
        qcResult.setStatus(UserConstants.ORDER_STATUS_FINISHED);
        //跟新检验表数据
        qcResultService.updateQcResult(qcResult);

        //检验记录数据表修改状态为检验完成
        QcResultRecord qcResultRecord = new QcResultRecord();
        qcResultRecord.setResultId(qcResult.getRecordId());
        //查找到检验完成行数据的所有检验记录数据
        List<QcResultRecord> qcResultRecords = qcResultRecordService.selectQcResultRecordList(qcResultRecord);
        for (QcResultRecord resultRecord : qcResultRecords) {
            resultRecord.setStatus("1");
            qcResultRecordService.updateQcResultRecord(resultRecord);
        }

        ProTask proTask = proTaskService.selectProTaskByTaskId(qcResult.getTaskId());
        if ("1".equals(proTask.getPlanEnableWarehous())){
            ProProductRecpt proProductRecpt = proProductRecptService.selectProProductRecptByRecptId(qcResult.getFeedbackId());
            //合格品数量
            proProductRecpt.setQuantityQualified(qcResult.getQuantityQualified());
            //不合格品数量
            proProductRecpt.setQuantityUnquanlified(qcResult.getQuantityUnquanlified());
            //待检测数量
            proProductRecpt.setQuantityUncheck(qcResult.getQuantityUncheck());
            proProductRecptService.updateProProductRecpt(proProductRecpt);
            return AjaxResult.success();
        }else if ("0".equals(proTask.getPlanEnableWarehous())){
            //报工表跟新合格和不合格数量
            ProFeedback feedback = proFeedbackService.selectProFeedbackByRecordId(qcResult.getFeedbackId());
            //合格品数量
            feedback.setQuantityQualified(qcResult.getQuantityQualified());
            //不合格品数量
            feedback.setQuantityUnquanlified(qcResult.getQuantityUnquanlified());
            //待检测数量
            feedback.setQuantityUncheck(qcResult.getQuantityUncheck());
            proFeedbackService.updateProFeedback(feedback);
            return AjaxResult.success();
        }else {
            return AjaxResult.error("生产任务没有此报工或者包装任务！");
        }
    }
}
